#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

#define SHMSIZE 1024

int main()
{
    key_t key = ftok(".", 1);
    int shmid = shmget(key, SHMSIZE, IPC_CREAT| 0666);

    pid_t pid = fork();
    if (pid == 0)
    {
        //process A
        char *shmaddr = (char*)shmat(shmid, NULL, 0);
        char buffer[] = "i am process A";
        strncpy(shmaddr,buffer,SHMSIZE);
        shmdt(shmaddr);
        exit(1);
    }
    else
    {
        //process B
        char *shmaddr = (char*)shmat(shmid, NULL, 0);
        wait(NULL);
        printf("%s\n",shmaddr);
        shmdt(shmaddr);
    }
    shmctl(shmid,IPC_RMID,NULL);

    return 0;
}